/**
 * 轮播图管理相关JavaScript
 */
document.addEventListener('DOMContentLoaded', function() {
    if (typeof jQuery === 'undefined') {
        console.error('jQuery is not loaded! Banner functionality will not work.');
        return;
    }
    
    // 使用jQuery
    jQuery(function($) {
        // 获取上下文路径
        var contextPath = $('meta[name="context-path"]').attr('content') || '';
        
        // 图片预览
        $('#imageFile').change(function() {
            if (this.files && this.files[0]) {
                var reader = new FileReader();
                reader.onload = function(e) {
                    $('#imagePreview img').attr('src', e.target.result);
                    $('#imagePreview').removeClass('d-none');
                }
                reader.readAsDataURL(this.files[0]);
            }
        });
        
        // 删除轮播图
        $('.delete-btn').click(function() {
            var id = $(this).data('id');
            if (confirm("确定要删除这个轮播图吗？")) {
                $.ajax({
                    url: contextPath + '/admin/banner/delete/' + id,
                    type: 'POST',
                    success: function(response) {
                        if (response.status === 1) {
                            alert(response.msg);
                            location.reload();
                        } else {
                            alert(response.msg);
                        }
                    },
                    error: function() {
                        alert('操作失败，请稍后再试');
                    }
                });
            }
        });
        
        // 修改轮播图状态
        $('.status-btn').click(function() {
            var id = $(this).data('id');
            var status = $(this).data('status');
            var statusText = status === 1 ? '启用' : '禁用';
            
            if (confirm("确定要" + statusText + "这个轮播图吗？")) {
                $.ajax({
                    url: contextPath + '/admin/banner/status/' + id + '/' + status,
                    type: 'POST',
                    success: function(response) {
                        if (response.status === 1) {
                            alert(response.msg);
                            location.reload();
                        } else {
                            alert(response.msg);
                        }
                    },
                    error: function() {
                        alert('操作失败，请稍后再试');
                    }
                });
            }
        });
        
        // 表单提交 - 新增
        $('#bannerAddForm').submit(function(e) {
            e.preventDefault();
            
            var formData = new FormData(this);
            
            // 处理复选框状态
            if (!$('#status').is(':checked')) {
                formData.set('status', '0');
            }
            
            $.ajax({
                url: contextPath + '/admin/banner/save',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    if (response.status === 1) {
                        alert(response.msg);
                        window.location.href = contextPath + '/admin/banner/list';
                    } else {
                        alert(response.msg);
                    }
                },
                error: function(xhr) {
                    console.log(xhr);
                    alert('保存失败，请稍后再试');
                }
            });
        });
        
        // 表单提交 - 编辑
        $('#bannerEditForm').submit(function(e) {
            e.preventDefault();
            
            var formData = new FormData(this);
            
            // 处理复选框状态
            if (!$('#status').is(':checked')) {
                formData.set('status', '0');
            }
            
            $.ajax({
                url: contextPath + '/admin/banner/update',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    if (response.status === 1) {
                        alert(response.msg);
                        window.location.href = contextPath + '/admin/banner/list';
                    } else {
                        alert(response.msg);
                    }
                },
                error: function(xhr) {
                    console.log(xhr);
                    alert('保存失败，请稍后再试');
                }
            });
        });
    });
}); 